/*
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-04-16 16:15:05
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-21 14:45:50
 * @FilePath: /free-learning-front/src/api/user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import request, { get, post } from "@/utils/request";
import type { UploadUserFile } from "element-plus";

const USER_BASE_URL = "/api/v1/users";

export interface UserExpertiseBO {
  expertiseId: string;
  expertiseName: string;
  expertiseCode: string;
}

export interface UserPageVO {
  id: number;
  username: string;
  nickname: string;
  mobile: string;
  gender: number;
  avatar: string;
  email: string;
  status: number;
  deptName: string;
  roleNames: string;
  createTime: string;
  academicDegreeId: string;
  academicDegreeCode: string;
  academicDegreeName: string;
  expertiseList: UserExpertiseBO[];
  schoolId: number;
  schoolName: string;
}

export const requestUserList = (code?: string): Promise<UserPageVO> => {
  return get<UserPageVO>(`${USER_BASE_URL}/consultant/${code}/list`);
};

export interface SingInForm {
  username: string;
  nickname: string;
  mobile: string;
  gender: number;
  password: string;
  avatar?: string;
  email?: string;
  status?: number;
  deptId?: number;
  roleIds: number[];
  academicDegreeCode?: string;
  academicDegreeName?: string;
  schoolId?: number;
  isConsultant?: boolean;
  schoolName?: string;
  emailCode?: string;
}
/** 用户表单类型 */
export interface SingInConsultantForm {
  /** 用户头像 */
  avatar?: string;
  /** 部门ID */
  deptId?: number;
  /** 邮箱 */
  email?: string;
  /** 性别 */
  gender?: number;
  /** 用户ID */
  id?: number;
  /** 手机号 */
  mobile?: string;
  /** 昵称 */
  nickname?: string;
  /** 角色ID集合 */
  roleIds?: number[];
  /** 用户状态(1:正常;0:禁用) */
  status?: number;
  /** 用户名 */
  username?: string;
  /** 擅长领域 */
  expertiseList?: string[];
  expertiseCodes?: string[] | { dictCode: string; name: string }[];
  /** 学位 */
  academicDegreeCode?: string;
  academicDegreeName?: string;
  /** 毕业院校ID */
  schoolId?: string;
  /** 毕业院校名称 */
  schoolName?: string;
  idCard?: string;
  /** 身份证正面照片 */
  idCardFrontUrl?: string;
  /** 身份证背面照片 */
  idCardBackUrl?: string;
  /** 学位证书照片 */
  degreeUrl?: string;
  /** 其他证书照片 */
  otherUrl?: (UploadUserFile & { fileName: string })[];
  isConsultant?: boolean;
  emailCode?: string;
}
export const requestUserRegister = (data: SingInForm): Promise<boolean> => {
  return post(`${USER_BASE_URL}/signIn`, data);
};
export const requestConsultantRegister = (
  data: SingInConsultantForm
): Promise<boolean> => {
  return post(`${USER_BASE_URL}/signIn`, data);
};
/** 登录用户信息 */
export interface UserInfo {
  /** 用户ID */
  userId?: number;

  /** 用户名 */
  username?: string;

  /** 昵称 */
  nickname?: string;

  /** 头像URL */
  avatar?: string;
}
/** 登录请求参数 */
export interface LoginData {
  /** 用户名 */
  username: string;
  /** 密码 */
  password: string;
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码 */
  captchaCode: string;
}
const AUTH_BASE_URL = "/api/v1/auth";
/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  accessToken: string;
  /** 刷新令牌 */
  refreshToken: string;
  /** 令牌类型 */
  tokenType: string;
  /** 过期时间(秒) */
  expiresIn: number;
}
export const login = (data: LoginData): Promise<LoginResult> => {
  const formData = new FormData();
  formData.append("username", data.username);
  formData.append("password", data.password);
  formData.append("captchaKey", data.captchaKey);
  formData.append("captchaCode", data.captchaCode);
  return request({
    url: `${AUTH_BASE_URL}/login`,
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

export const getInfo = (): Promise<UserInfo> => {
  return request<any, UserInfo>({
    url: `${USER_BASE_URL}/me`,
    method: "get",
  });
};

export const logout = (): Promise<void> => {
  return request({
    url: `${AUTH_BASE_URL}/logout`,
    method: "delete",
  });
};

/** 验证码响应 */
export interface CaptchaResult {
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码图片Base64字符串 */
  captchaBase64: string;
}
/** 获取验证码接口*/
export const getCaptcha = (): Promise<CaptchaResult> => {
  return request({
    url: `${AUTH_BASE_URL}/captcha`,
    method: "get",
  });
};
/** 个人中心用户信息 */
export interface UserProfileVO {
  /** 用户ID */
  id?: number;

  /** 用户名 */
  username?: string;

  /** 昵称 */
  nickname?: string;

  /** 头像URL */
  avatar?: string;

  /** 性别 */
  gender?: number;

  /** 手机号 */
  mobile?: string;

  /** 邮箱 */
  email?: string;

  /** 创建时间 */
  createTime?: Date;

  /** 学位 */
  academicDegreeCode?: string;
  academicDegreeName?: string;

  /** 学校ID */
  schoolId?: number;

  /** 学校名称 */
  schoolName?: string;
}
export const queryProfile = (): Promise<UserPageVO> => {
  return get<UserPageVO>(`${USER_BASE_URL}/profile`);
};

export interface PasswordChangeForm {
  /** 原密码 */
  oldPassword?: string;
  /** 新密码 */
  newPassword?: string;
  /** 确认新密码 */
  confirmPassword?: string;
}

/** 修改邮箱表单 */
export interface EmailBindingForm {
  /** 邮箱 */
  email?: string;
  /** 验证码 */
  code?: string;
}
/**
 *   发送邮箱验证码
 *
 * @param contact 联系方式  邮箱
 * @param contactType 联系方式类型 EMAIL:邮箱
 */
export const sendVerificationCode = (contact: string, contactType: string) => {
  return request({
    url: `${USER_BASE_URL}/send-verification-code`,
    method: "post",
    params: { contact: contact, contactType: contactType },
  });
};
/** 修改用户密码 */
export const changePassword = (data: PasswordChangeForm) => {
  return request({
    url: `${USER_BASE_URL}/password`,
    method: "put",
    data: data,
  });
};

export const changeEmail = (data: EmailBindingForm) => {
  return request({
    url: `${USER_BASE_URL}/email`,
    method: "put",
    data: data,
  });
};
export const getConsultantInfo = (
  consultantId: number
): Promise<UserPageVO & { expertise: UserExpertiseBO[] }> => {
  return request({
    url: `${USER_BASE_URL}/consultant/${consultantId}`,
    method: "get",
  });
};
